﻿using System.Data.Entity;

namespace MvcApplication2.Models
{
    public class MvcApplication2Context:DbContext
    {
        public MvcApplication2Context()
            : base("name=MvcApplication2Context")
        {
        }

        public DbSet<Nhanvien> Nhanviens { get; set; }

        public DbSet<Khachhang> Khachhangs { get; set; }

        public DbSet<Tintuc> Tintucs { get; set; }

        public DbSet<Ykien> Ykiens { get; set; }

        public DbSet<Nhacungcap> Nhacungcaps { get; set; }

        public DbSet<Loaisanpham> Loaisanphams { get; set; }

        public DbSet<Sanpham> Sanphams { get; set; }

        public DbSet<Thongtinsanpham> Thongtinsanphams { get; set; }

        public DbSet<Hoadon> Hoadons { get; set; }

        public DbSet<Chitiethoadonsanpham> Chitiethoadonsanphams { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Chitiethoadonsanpham>()
                        .HasKey(ct => new { ct.HoadonID, ct.SanphamID });

            modelBuilder.Entity<Thongtinsanpham>().HasKey(tt => new { tt.ThongtinspID });

            modelBuilder.Entity<Hoadon>()
                        .HasMany(h => h.Chitiethoadonsanphams)
                        .WithRequired()
                        .HasForeignKey(ct => ct.HoadonID);

            modelBuilder.Entity<Sanpham>()
                        .HasMany(s => s.Chitiethoadonsanphams)
                        .WithRequired()
                        .HasForeignKey(ct => ct.SanphamID);

            modelBuilder.Entity<Sanpham>().HasMany(s => s.Thongtinsanphams).WithRequired().HasForeignKey(tt => tt.SanphamID);
        }
    }
}